﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Solution35
{
    public int SearchInsert(int[] nums, int target)
    {
        int len=nums.Length;
       
        int centerIndex = len / 2 ;
        int left = 0;
        int right = len - 1;
        int center = nums[centerIndex];
        int ans = len;
        while (left<right)
        {
            if (target < center)
            {
                right = centerIndex-1;
                
            }
            else if(target > center)
            {
               
                left = centerIndex+1;


            }
            else{
                return centerIndex;//center=mid
            }
            centerIndex = (left + right) / 2;
            center = nums[centerIndex];
        }
        return left;

    }
}
class Program35
{
    static void Main35()
    {
        Solution35 solution = new Solution35();


        int[] nums = { 1, 3, 5, 6 };
        int res =solution.SearchInsert(nums, 2);
        Console.WriteLine(res);
        Console.ReadKey();
    }
}
